package conf

import (
	"fmt"
	"github.com/jinzhu/gorm"
	"github.com/spf13/viper"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"log"
)

var Mdb *gorm.DB

func InitDB() error{
	dsn := fmt.Sprintf("%s:%s@(%s)/%s?charset=%s&parseTime=True&loc=Local", viper.GetString("app.db.username"),
		viper.GetString("app.db.password"), viper.GetString("app.db.host")+":"+viper.GetString("app.db.port"),
		viper.GetString("app.db.database"), "utf8")
	db, err := gorm.Open("mysql", dsn)
	if err != nil {
		log.Panic("数据库连接错误...", err)
		return err
	}
	Mdb = db

	return nil
}
